Skip to content

LocalCSE: Optimize idempotent functions#8383

Merged
kripken merged 12 commits intoWebAssembly:mainfrom
kripken:localcse.idempotent
Feb 26, 2026
Merged

LocalCSE: Optimize idempotent functions#8383
kripken merged 12 commits intoWebAssembly:mainfrom
kripken:localcse.idempotent

Conversation

@kripken
Copy link
Member

@kripken kripken commented Feb 25, 2026

As a drive-by, add a test/lit/idempotent.wast test that was
missing, for binary format testing of that hint.

@kripken kripken requested a review from tlively February 25, 2026 21:23
;; RUN: wasm-opt -all %s -S -o - | filecheck %s
;; RUN: wasm-opt -all --roundtrip %s -S -o - | filecheck %s

;; Test text and binary handling of @binaryen.idempotent.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Worth testing the call site annotation as well?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, it is actually a little tedious to check it (need to be careful to check the right one, and remember the value), so I was thinking to just not optimize it, but forgot to add a comment with a TODO. I added one now.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense for CSE, but we could still test for the round tripping, right?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh right, added.


bool isIdempotent(Expression* curr, Module& wasm) {
if (auto* call = curr->dynCast<Call>()) {
if (Intrinsics::getAnnotations(wasm.getFunction(call->target)).idempotent) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't we support annotations on the call sites as well?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(see above, sorry for the confusion)

Comment on lines 203 to 206
(i32.eq
(global.get $mutable)
(global.get $mutable)
)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be clearer to do (i32.add (global.get $mutable) (i32.const 1)) or similar. My first thought when I saw this test was confusion because the i32.eqs are obviously optimizable - just not by LocalCSE.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed to i32.add (that seems obvious enough, even without an i32.const? the const adds more stuff to read, and the need to see the constant value does not change)

@kripken kripken merged commit 66bf26b into WebAssembly:main Feb 26, 2026
17 checks passed
@kripken kripken deleted the localcse.idempotent branch February 26, 2026 00:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants